package one.stand.service.impl;

import com.alibaba.fastjson.JSONObject;
import one.stand.entity.ResultTemplateEntity;
import one.stand.model.CompanyAccountModel;
import one.stand.model.ResultModel;
import one.stand.request.CompanyAccountRequest;
import one.stand.request.live.LiveRequest;
import one.stand.response.live.LiveResponse;
import one.stand.service.CompanyAccontService;
import one.stand.service.LiveService;
import one.stand.service.WxMessageService;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.io.IOException;

/**
 * 小程序直播业务实现类
 * @author luoyang
 * @date 2020/5/19 13:43
 */
@Service
@Transactional
public class LiveServiceImpl extends PublicBaseService implements LiveService {
    @Autowired
    private WxMessageService wxMessageService;

    @Autowired
    protected CompanyAccontService companyAccontService;

    @Override
    public ResultModel queryLiveList(LiveRequest request){
        // 从商品的company_id 查找company表中user_id，然后再查找user表中user_id对应数据的enterprise_id，最终确定真正小程序的company_id
        Integer companyId = companyMapper.selectEnterpriseIdByCompanyId(request.getCId().intValue());
        CompanyAccountModel companyAccountModel= companyAccontService.selectByCompanyKeyDefault(new CompanyAccountRequest(){{
            setCompanyId(companyId);
        }}) ;
        String token = wxMessageService.getWxToken(companyAccountModel.getAppId(),companyAccountModel.getAppSecret());
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String url = "https://api.weixin.qq.com/wxa/business/getliveinfo?access_token=".concat(token);
        HttpPost httpPost = new HttpPost(url);
        // 装配post请求参数
        JSONObject json = new JSONObject();
        json.put("start", request.getStart());
        json.put("limit", request.getLimit());
        try {
            log.info("queryLiveList:{}",json);
            StringEntity myEntity = new StringEntity(json.toJSONString(), ContentType.APPLICATION_JSON);
            // 设置post求情参数
            httpPost.setEntity(myEntity);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                // 查询成功
                String resutlEntity = EntityUtils.toString(httpResponse.getEntity());
                log.info("查询成功：{}", resutlEntity);
                LiveResponse liveResponse = JSONObject.parseObject(resutlEntity, LiveResponse.class);
                return ResultModel.success(liveResponse);
            } else {
                return ResultModel.fail();
            }
        } catch (Exception e) {
            log.error("微信[异常]", e);
        } finally {
            try {
                if (httpClient != null) {
                    // 释放资源
                    httpClient.close();
                }
            } catch (IOException e) {
                log.error("释放连接资源[异常]", e);
            }
        }
        return ResultModel.fail();
    }

}
